import { ref, provide, inject, readonly } from 'vue'

const SNACKBAR_SYMBOL = Symbol('snackbar')

export function createSnackbar() {
  const isVisible = ref(false)
  const message = ref('')
  const color = ref('success')
  const timeout = ref(3000)

  function show(options) {
    message.value = options.message
    color.value = options.color || 'success'
    timeout.value = options.timeout || 3000
    isVisible.value = true
  }

  function success(message, timeout = 3000) {
    show({ message, color: 'success', timeout })
  }

  function error(message, timeout = 3000) {
    show({ message, color: 'error', timeout })
  }

  function warning(message, timeout = 3000) {
    show({ message, color: 'warning', timeout })
  }

  function info(message, timeout = 3000) {
    show({ message, color: 'info', timeout })
  }

  const snackbar = {
    isVisible: readonly(isVisible),
    message: readonly(message),
    color: readonly(color),
    timeout: readonly(timeout),
    show,
    success,
    error,
    warning,
    info
  }

  provide(SNACKBAR_SYMBOL, snackbar)
  return snackbar
}

export function useSnackbar() {
  const snackbar = inject(SNACKBAR_SYMBOL, null)
  if (!snackbar) {
    // 返回一个空的实现，而不是抛出错误
    return {
      isVisible: ref(false),
      message: ref(''),
      color: ref('success'),
      timeout: ref(3000),
      show: () => {},
      success: () => {},
      error: () => {},
      warning: () => {},
      info: () => {}
    }
  }
  return snackbar
} 